Slip 20


Q.1. Implement Ridge Regression and Lasso regression model using boston_houses.csv  
and take only ‘RM’ and ‘Price’ of the houses. Divide the data as training and testing  
data. Fit line using Ridge regression and to find price of a house if it contains 5 rooms 
and compare results.

# Import required libraries
import pandas as pd
from sklearn.linear_model import Ridge, Lasso, ElasticNet
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Load the dataset
data = pd.read_csv('boston_houses.csv')  # Ensure this CSV is in the same directory or give full path

# Use only 'RM' (rooms) and 'Price'
X = data[['RM']]
y = data['Price']

# Split the dataset into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Fit Ridge Regression
ridge_model = Ridge(alpha=1.0)
ridge_model.fit(X_train, y_train)
ridge_predictions = ridge_model.predict(X_test)

# Fit Lasso Regression
lasso_model = Lasso(alpha=0.1)
lasso_model.fit(X_train, y_train)
lasso_predictions = lasso_model.predict(X_test)

# Fit ElasticNet Regression
elastic_model = ElasticNet(alpha=0.1, l1_ratio=0.5)
elastic_model.fit(X_train, y_train)
elastic_predictions = elastic_model.predict(X_test)

# Predict price for a house with 5 rooms using proper feature name
input_data = pd.DataFrame({'RM': [5]})
ridge_price = ridge_model.predict(input_data)[0]
lasso_price = lasso_model.predict(input_data)[0]
elastic_price = elastic_model.predict(input_data)[0]

# Print model evaluations and predictions
print("----- Ridge Regression -----")
print("MSE:", mean_squared_error(y_test, ridge_predictions))
print("Predicted price for 5 rooms:", ridge_price)

print("\n----- Lasso Regression -----")
print("MSE:", mean_squared_error(y_test, lasso_predictions))
print("Predicted price for 5 rooms:", lasso_price)

print("\n----- ElasticNet Regression -----")
print("MSE:", mean_squared_error(y_test, elastic_predictions))
print("Predicted price for 5 rooms:", elastic_price)

Q.2. Write a python program to implement Decision Tree whether or not to play Tennis. 

# Import necessary libraries
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt

# Step 1: Create the dataset
data = {
    'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast',
                'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
    'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool',
                    'Mild', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
    'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal',
                 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
    'Windy': ['False', 'True', 'False', 'False', 'False', 'True', 'True',
              'False', 'False', 'False', 'True', 'True', 'False', 'True'],
    'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes',
             'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}

df = pd.DataFrame(data)
print("Original Dataset:\n", df, "\n")

# Step 2: Encode categorical values into numeric format
le = LabelEncoder()
for col in df.columns:
    df[col] = le.fit_transform(df[col])

print("Encoded Dataset:\n", df, "\n")

# Step 3: Split dataset into features and target
X = df[['Outlook', 'Temperature', 'Humidity', 'Windy']]
y = df['Play']

# Step 4: Create and train the Decision Tree model
model = DecisionTreeClassifier(criterion='entropy', random_state=42)
model.fit(X, y)

# Step 5: Visualize the Decision Tree
plt.figure(figsize=(10, 6))
plot_tree(model, feature_names=['Outlook', 'Temperature', 'Humidity', 'Windy'],
          class_names=['No', 'Yes'], filled=True, rounded=True)
plt.title("Decision Tree - Play Tennis")
plt.show()

# Step 6: Example prediction
# Example: Outlook=Sunny, Temperature=Cool, Humidity=High, Windy=False
example = [[le.transform(['Sunny'])[0],
            le.transform(['Cool'])[0],
            le.transform(['High'])[0],
            le.transform(['False'])[0]]]

predicted = model.predict(example)
print("Example Input → Outlook=Sunny, Temperature=Cool, Humidity=High, Windy=False")
print("Predicted Play Decision:", "Yes" if predicted[0] == 1 else "No")